package org.example.musicserver.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.example.musicserver.common.Result;
import org.example.musicserver.entity.Rate;
import org.example.musicserver.mapper.RateMapper;
import org.example.musicserver.service.RateService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RateServiceImpl extends ServiceImpl<RateMapper, Rate> implements RateService {

    @Resource
    private RateMapper rateMapper;

    @Override
    public Result addOrUpdateRate(Rate rate) {

        if(rate.getId() != null){
            rateMapper.updateById(rate);
        }else {
            rateMapper.insert(rate);
        }

        return Result.success("操作成功");
    }

    @Override
    public Result detail(Rate rate) {
        QueryWrapper<Rate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",rate.getUserId()).eq("type",rate.getType()).eq("rate_id",rate.getRateId());

        List<Rate> rates = rateMapper.selectList(queryWrapper);
        if(rates.isEmpty()){
            return Result.success("返回结果成功");
        }

        return Result.success("返回结果成功",rates.get(0));
    }
}
